import matplotlib.pyplot as plt


def plot_grid(edges_filename, dofs_filename):
    # 读取点坐标信息
    points = {}
    with open(dofs_filename) as f:
        for i, line in enumerate(f):
            x, y = map(float, line.strip()[1:-1].split(','))
            points[i] = (x, y)

    # 读取边信息
    edges = []
    with open(edges_filename) as f:
        for line in f:
            start, end = line.strip().split()
            start_x, start_y = map(float, start[1:-1].split(','))
            end_x, end_y = map(float, end[1:-1].split(','))
            edges.append(((start_x, start_y), (end_x, end_y)))

    # 绘制网格
    for (start_x, start_y), (end_x, end_y) in edges:
        plt.plot([start_x, end_x], [start_y, end_y], color='black')

    # 标记网格点
    for i, (x, y) in points.items():
        plt.plot(x, y, 'bo')
        plt.annotate(str(i), xy=(x, y), xytext=(5, 5),
                     textcoords='offset points')

    # 设置图形范围和标题
    # plt.xlim(0, 1)
    # plt.ylim(0, 1)
    plt.title('Grid')

    # 显示图形
    plt.show()


if __name__ == "__main__":
    edges_filename = "edges_refine.txt"
    dofs_filename = "dofs_refine.txt"
    plot_grid(edges_filename, dofs_filename)
